package com.zzw.basic.java;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {

	public static void main(String[] args){ 
	
		Callable<Integer> callable = new Callable<Integer>() {
	        public Integer call() throws Exception {
	            return new Random().nextInt(100);
	        }
	    };
	    FutureTask<Integer> future = new FutureTask<Integer>(callable);
	    new Thread(future).start();
	    try {
	        Thread.sleep(1000);
	        System.out.println(future.get());
	    } catch (InterruptedException e) {
	        e.printStackTrace();
	    } catch (ExecutionException e) {
	        e.printStackTrace();
	    }
	    /*	final ExecutorService exec = Executors.newFixedThreadPool(5);  
			Callable<String> call = new Callable<String>() {  
			    public String call() throws Exception {  
			        Thread.sleep(1000 * 10);//����ָ����ʱ�䣬�˴���ʾ�ò����ȽϺ�ʱ  
			        return "Other less important but longtime things.";  
			    }  
			};  
			Future<String> task = exec.submit(call);  
			//��Ҫ������  
			System.out.println("Let's do important things. start");  
			Thread.sleep(1000 * 3);  
			System.out.println("Let's do important things. end");  
			
			//����Ҫ������  
			while(! task.isDone()){  
			    System.out.println("still waiting....");  
			    Thread.sleep(1000 * 1);  
			}  
			System.out.println("get sth....");  
			String obj = task.get();  
			System.out.println(obj);  
			//�ر��̳߳�  
			exec.shutdown();
	     */  
		}  
}